package edu.hawaii.ics466;

import java.util.concurrent.TimeUnit;

import android.app.ActionBar;
import android.app.ListActivity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import edu.hawaii.ics466.tasks.GetGameList;

public class GameList extends ListActivity {

	private static final int MENU_LOGIN = 0;
	private static final int MENU_LOGOUT = 1;
	private static final int MENU_FAVORITES = 2;
	private static final int MENU_SETTINGS = 3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			ActionBar actionBar = getActionBar();
			actionBar.setDisplayHomeAsUpEnabled(true);

			String query = intent.getStringExtra(SearchManager.QUERY);
			doSearch(query);
		} else {
			doSearch("");
		}
	}

	@Override
	public void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			String query = intent.getStringExtra(SearchManager.QUERY);
			doSearch(query);
		}
	}

	@Override
	public void onListItemClick(ListView listView, View view, int position, long id) {
		Intent intent = new Intent(this, GameView.class);
		String itemLabel = listView.getItemAtPosition(position).toString();
		intent.putExtra(GameView.INTENT_KEY_GAME_NAME, itemLabel);
		startActivity(intent);
		super.onListItemClick(listView, view, position, id);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Use inflater to add SearchView Widget
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);

		// Link SearchView Widget
		SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		ComponentName componentName = getComponentName();
		searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));

		// Add menu items
		if (Registry.username == null) {
			menu.add(Menu.NONE, MENU_LOGIN, Menu.NONE, "Login");
		} else {
			menu.add(Menu.NONE, MENU_LOGOUT, Menu.NONE, "Logout");
			menu.add(Menu.NONE, MENU_FAVORITES, Menu.NONE, "Favorites");
		}
		menu.add(Menu.NONE, MENU_SETTINGS, Menu.NONE, "Settings");

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				break;
			case MENU_LOGIN:
				Toast.makeText(this, "login", Toast.LENGTH_SHORT).show();
				break;
			case MENU_LOGOUT:
				break;
			case MENU_FAVORITES:
				break;
			case MENU_SETTINGS:
				break;
		}
		return true;
	}

	private void doSearch(String query) {
		String[] gameList = { "Failed to get Game List." };
		String[] requestParams = { query };
		try {
			gameList = new GetGameList().execute(requestParams).get(2, TimeUnit.SECONDS);
			setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, gameList));
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "failed to get game list.", Toast.LENGTH_SHORT).show();
		}
	}
}